﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Autofac.Extras.DynamicProxy;
using EFCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using StackExchange.Profiling;
using WebApplication2.Aop;
using WebApplication2.UploadFile;

namespace WebApplication2.Controllers
{
    [Intercept(typeof(TestInterceptor))]
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        public static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        public  readonly ILogger<WeatherForecastController> _logger;
        //会自动给你注入进去
        public IUploadFile _uploadFile { get; set; }    

        public IUploadFile UploadFile { get; set; }
        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }


        /// <summary>
        /// 方法注释
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }



        /// <summary>
        /// 添加组织
        /// </summary>
        [HttpPost]
        [Route("AddGroup")]
        public void AddGroup()
        {
            using (var db = new CoreDbContext())
            {
                GroupModel model = new GroupModel();

                model.PID = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;

                db.Add<GroupModel>(model);
                db.SaveChanges();
            }
        }



        /// <summary>
        /// 获得所有组织
        /// https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-how-to?view=netcore-3.1   Text.json的文档
        /// </summary>
        [HttpGet]
        [Route("getGroup")]
        public string GetGroup()
        {
            using (var db = new CoreDbContext())
            {
                GroupModel model = new GroupModel();

                model.PID = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;

                var query = from a in db.Set<GroupModel>()
                            select a;

                var models = query.ToList();

                return JsonSerializer.Serialize(models);
            }
        }

        /// <summary>
        /// 还没起作用
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("testMiniprofilter")]
        public string TestMiniprofilter()
        {
            using (MiniProfiler.Current.Step("获取成功后，开始处理最终数据"))
            {
                using (var db = new CoreDbContext())
                {
                    GroupModel model = new GroupModel();

                    model.PID = Guid.NewGuid().ToString();
                    model.CreateTime = DateTime.Now;

                    var query = from a in db.Set<GroupModel>()
                                select a;

                    var models = query.ToList();

                    return JsonSerializer.Serialize(models);
                }
            }

        }

        /// <summary>
        /// https://autofaccn.readthedocs.io/zh/latest/integration/aspnetcore.html
        /// autofac的官方文档  通过实例将实例注入的方式写好
        /// 测试net core的DI注入 和auto-face 
        /// </summary>
        [HttpGet]
        [Route("testDI")]
        public string TestDI()
        {
            //使用原生的依赖注入 只能通过构造函数的方式来进行
            return _uploadFile.UploadFileFiles();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("testAutoFac")]
        public string TestAutoFac()
        {
            return UploadFile.UploadFileFiles();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("testAop")]
        public virtual string TestAutoAop()
        {
            return UploadFile.UploadFileFiles();
        }


    }
}
